﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
	public class listActivityOutput : Api_BaseOutput
	{
		public List<listActivityResults> result { get; set; }
		public listActivityParms inputParms { get; private set; }
		//
		public listActivityOutput( listActivityInput input )
			: base( input )
		{
			inputParms = input.parms;
			result = new List<listActivityResults>();
		}
		// --------------------------------------------------- tostring
		public override string ToString()
		{
			return string.Format( "List: {0}, days returned: {1}",
				this.inputParms.id, result.Count );
		}

		#region ================================================================== format setup
		// ---- serial
		internal void Format( string api_Request, string api_Response )
		{
			this.api_Request = api_Request;
			this.api_Response = api_Response;
			// ----------------------------------
			if ( this.api_OutputType == EnumValues.OutputType.JSON )
				outputJSON();
			else if ( this.api_OutputType == EnumValues.OutputType.XML )
				outputXML();
		}
		// ---- xmlrpc
		internal void Format( x_listActivity[] rpcResult )
		{
			this.api_Request = EnumValues.NotSupplied;
			this.api_Response = EnumValues.NotSupplied;
			// ------------------------
			this.result = FormatResults( rpcResult );
		}
		#endregion

		#region  ===================================================================== format result
		// ---------------------------------------------------------- outputXML
		protected void outputXML()
		{
			XElement xml = XElement.Parse( this.api_Response );
			// error check
			Api_Error error = apiSerial.GetXmlOutputError( xml );
			if ( error != null )
			{
				this.api_ErrorMessages.Add( error );
				return;
			}
			// -------------------------------------------- get values
			DateTime dt; double dbl;
			try
			{
				IEnumerable<XElement> activities = xml.Elements( "struct" );
				foreach ( XElement item in activities )
				{
					listActivityResults entry = new listActivityResults();
					// ----------------------------------
					DateTime.TryParse( item.Element( "day" ).Value, out dt );
					entry.day = dt;
					//
					double.TryParse( item.Element( "emails_sent" ).Value, out dbl );
					entry.emails_sent = dbl;
					//
					double.TryParse( item.Element( "unique_opens" ).Value, out dbl );
					entry.unique_opens = dbl;
					//
					double.TryParse( item.Element( "recipient_clicks" ).Value, out dbl );
					entry.recipient_clicks = dbl;
					//
					double.TryParse( item.Element( "hard_bounce" ).Value, out dbl );
					entry.hard_bounce = dbl;
					//
					double.TryParse( item.Element( "soft_bounce" ).Value, out dbl );
					entry.soft_bounce = dbl;
					//
					double.TryParse( item.Element( "abuse_reports" ).Value, out dbl );
					entry.abuse_reports = dbl;
					//
					double.TryParse( item.Element( "subs" ).Value, out dbl );
					entry.subs = dbl;
					//
					double.TryParse( item.Element( "unsubs" ).Value, out dbl );
					entry.unsubs = dbl;
					//
					double.TryParse( item.Element( "other_adds" ).Value, out dbl );
					entry.other_adds = dbl;
					//
					double.TryParse( item.Element( "other_removes" ).Value, out dbl );
					entry.other_removes = dbl;
					//------------------- 
					this.result.Add( entry );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// ---------------------------------------------------------- outputJSON
		protected void outputJSON()
		{
			JavaScriptSerializer jss = new JavaScriptSerializer();

			try
			{
				if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
				{
					Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
					this.api_ErrorMessages.Add( err );
				}
				else
				{
					x_listActivity[] jsonResult = jss.Deserialize<x_listActivity[]>( this.api_Response );
					this.result = FormatResults( jsonResult );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// -----------------------------------------------------
		protected List<listActivityResults> FormatResults( x_listActivity[] x_results )
		{
			List<listActivityResults> results = new List<listActivityResults>();
			//
			DateTime dt;
			// array to generic list
			foreach ( x_listActivity xact in x_results )
			{
				listActivityResults entry = new listActivityResults();
				//
				DateTime.TryParse( xact.day, out dt );
				entry.day = dt;
				//
				entry.emails_sent = xact.emails_sent;
				entry.unique_opens = xact.unique_opens;
				entry.recipient_clicks = xact.recipient_clicks;
				entry.hard_bounce = xact.hard_bounce;
				entry.soft_bounce = xact.soft_bounce;
				entry.abuse_reports = xact.abuse_reports;
				entry.subs = xact.subs;
				entry.unsubs = xact.unsubs;
				entry.other_adds = xact.other_adds;
				entry.other_removes = xact.other_removes;
				//
				results.Add( entry );
			}
			return results;
		}
		//
		#endregion
	}
}
